<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>树组件</title>

	</head>
		<script src="../js/global.js" type="text/javascript" charset="utf-8"></script>
<script>
			layui.use(['tree', 'laydate'], function() {
				var tree = layui.tree;
				var $ = layui.$;
				$.post(globalData.server + "GetAllModuleLayUI", {
					token: globalData.myToken
				}, function(res) {
					console.log(res);
					//渲染
					var inst1 = tree.render({
						elem: ' #test1', //绑定元素
						edit: ['add', 'update', 'del'],
						data: res,
						id:'demoId',

						operate: function(obj) {
							var type = obj.type; //得到操作类型：add、edit、del
							var data = obj.data; //得到当前节点的数据
							var elem = obj.elem; //得到当前节点元素
							//Ajax 操作
							var id = data.id; //得到节点索引
							if(type === 'add') { //增加节点

								//返回 key 值
								
								$.post(globalData.server + "CreateModule", {
									mId: 1005,
									name: '我是新来的',
									parentId: 96,
									path: 1,
									weight: 1,
									token: globalData.myToken
								}, 'json');
								
								return '我是新来的';
							} else if(type === 'update') { //修改节点
								//console.log(elem.find('.layui-tree-txt').html()); //得到修改后的内容
								//获取名字
								var name1 = elem.find('.layui-tree-txt').html();
								console.log(name1);
								//获取父id
								var id1 = elem.parent().parent().data().id;
								console.log(id1);

								$.post(globalData.server + "UpdateModule", {
									mId: id,
									name: name1,
									parentId: id1,
									path: 1,
									weight: 1,
									token: globalData.myToken
								}, 'json');

							} else if(type === 'del') { //删除节点
								console.log(id);
								$.post(globalData.server + "DeleteModule", {
									mId: id,
									token: globalData.myToken
								});
							};
						},
						id: 'demoId'
					});
				}, 'json');
			});
		</script>


	<body>
		<div id="test1"></div>
		
		<div id="dataForm">
			
		</div>

		

	</body>

</html>